package activator;

import library.service.Book;
import library.service.LibraryService;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

public class Activator implements BundleActivator {

	ServiceReference serviceRef;
	LibraryService libraryService;

	public void start(BundleContext context) throws Exception {
		serviceRef = context
				.getServiceReference(LibraryService.class.getName());

		if (serviceRef != null) {
			libraryService = (LibraryService) context.getService(serviceRef);
			if (libraryService != null) {
				Book foundBook = libraryService.findBookByName("Java");
				foundBook.setName("Java Masters");
				System.out.println("CONSUMER 1 : Book name changed.");
			}
		}

	}

	public void stop(BundleContext context) throws Exception {
		if (libraryService != null) {
			context.ungetService(serviceRef);
			libraryService = null;
			serviceRef = null;
		}

	}

}
